package org.goodsl.mdd.validator;

import org.goodsl.mdd.Seg;
import org.goodsl.mdd.SingleValidResult;
import org.goodsl.mdd.Validator;

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 12-11-14
 * Time: 下午1:57
 * To change this template use File | Settings | File Templates.
 */
public class AllowNullValidator extends Validator<Seg> {

    private static AllowNullValidator instance = new AllowNullValidator();
    public static  AllowNullValidator getInstance(){
        return  instance;
    }


    public AllowNullValidator(){
        super("");
    }
    @Override
    public SingleValidResult<Seg> verify(Seg variable) {

        boolean isNull = false;

        Object value = variable.getValue();
        if (value == null)
            isNull = true;
        if(value instanceof String){
            if(((String) value).trim().length() == 0)
                isNull = true;
            else
                isNull = false;

        }

        if(isNull)
            return new SingleValidResult<Seg>(true,"",variable,this);
        else
            return new SingleValidResult<Seg>(false,getMessage(),variable,this);
    }
}
